﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.Ajax.Utilities;
using PMS.IServices;
using PMS.Models.Base;
using PMS.Services;
using PMS.Utils;

namespace PMS.EasyUI.Controllers
{
    public class HomeController : Controller
    {
        IBaseUserService _baseUserService = new BaseUserService();
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        /// <summary>
        /// 用户登陆页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Login()
        {
            return View();
        }

        /// <summary>
        /// 登陆请求页面
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Login(BaseUser user)
        {
            string userName = Request.Form["username"] ?? "";
            string passWord = Request.Form["password"] ?? "";
            if (_baseUserService.IsExistUser(userName, passWord))
            {
                BaseUser baseUser = _baseUserService.GetBaseUser(userName, passWord);
                Session["UserId"] = baseUser.Id;
                Session["UserName"] = baseUser.UserName;
                return Json(Result.Success, "text/html", JsonRequestBehavior.AllowGet);
            }

            return Json(Result.Error, "text/html", JsonRequestBehavior.AllowGet);
        }
    }
}